#!/bin/bash
set -e
HERE=`pwd`
ROOTFS=/root/opensuse152
OPENSUSE_MIRROR=http://download.opensuse.org
OPENSUSE=${OPENSUSE_MIRROR}/distribution/leap/15.2/repo/oss/
OPENSUSE_UPDATES=${OPENSUSE_MIRROR}/update/leap/15.2/oss/
#----------------------------------------------------------------------#
fct_check_uid()
{
  if [ $UID != 0 ]; then
    echo you must be root
    exit -1
  fi
}
#----------------------------------------------------------------------#
fct_check_losetup()
{
  modprobe loop
  if [[ "$(losetup -a )" != "" ]]; then
    echo
    echo
    echo losetup -a should give nothing, it gives:
    losetup -a
    echo
    exit -1
  fi
}
#----------------------------------------------------------------------#
fct_create_32G_mount_wkmntloops()
{
  truncate --size 32G ${ROOTFS}
  sgdisk --clear \
         --new 1::+1M --typecode=1:ef02 --change-name=1:'BIOS' \
         --new 2::-0 --typecode=2:8300 --change-name=2:'Linux' \
         ${ROOTFS}
  losetup /dev/loop0 ${ROOTFS}
  partprobe /dev/loop0
  mkfs.ext4 -F -L "cloonix" /dev/loop0p2
  mkdir -p /tmp/wkmntloops
  mount /dev/loop0p2 /tmp/wkmntloops
}
#----------------------------------------------------------------------#
fct_umount_wkmntloops()
{
  umount /tmp/wkmntloops
  losetup -d /dev/loop0
}
#----------------------------------------------------------------------#

#########################################################################
fct_check_uid
set +e
for i in qemu-img rpm yum blkid wget sgdisk partprobe; do
  path_bin=$(which $i)
  if [ -z $path_bin ]; then
    echo $i  DOES NOT EXIST
    exit -1
  fi
done

if [ -e $ROOTFS ]; then
  echo $ROOTFS already exists
  exit 1
fi

fct_check_losetup
set -e
fct_create_32G_mount_wkmntloops $ROOTFS
#-----------------------------------------------------------------------#
set +e
for i in ${OPENSUSE} ${OPENSUSE_UPDATES};
do
  wget --no-check-certificate --delete-after ${i} 1>/dev/null 2>&1
  OK=$?
  if [ "$OK" != "0" ]; then
    echo ERROR wget ${i}
    exit 1
  fi
  echo
  echo REPO OK ${i}
  echo
done
set -e
#-----------------------------------------------------------------------#
cd /tmp/wkmntloops
mkdir -p /tmp/wkmntloops/etc/yum.repos.d
cat > /tmp/wkmntloops/etc/yum.repos.d/opensuse.repo << EOF
[opensuse]
name=opensuse-15-2 - Base
baseurl=${OPENSUSE}
enabled=1
gpgcheck=0
EOF
#-----------------------------------------------------------------------#
cat > /tmp/wkmntloops/etc/yum.repos.d/opensuse-updates.repo << EOF
[updates]
name=opensuse-15-2 - Updates
baseurl=${OPENSUSE_UPDATES}
enabled=1
gpgcheck=0
EOF
#-----------------------------------------------------------------------#
yum --installroot=/tmp/wkmntloops -y install systemd
yum --installroot=/tmp/wkmntloops -y install zypper
#-----------------------------------------------------------------------#
if [ -e /etc/resolv.conf ]; then
  cp -f /etc/resolv.conf "/tmp/wkmntloops/etc"
fi
#-----------------------------------------------------------------------#
list_pkt="kernel-default grub2-i386-pc udhcp iproute2 "
list_pkt+="openssh xauth sudo kbd vim net-tools "
list_pkt+="iw dracut"
for d in dev sys proc; do mount --bind /$d /tmp/wkmntloops/$d; done
chroot /tmp/wkmntloops/ zypper addrepo ${OPENSUSE} 15.2-oss
chroot /tmp/wkmntloops/ zypper addrepo ${OPENSUSE_UPDATES} 15.2-oss-updates
chroot /tmp/wkmntloops/ zypper lr
chroot /tmp/wkmntloops/ zypper mr -r 1
chroot /tmp/wkmntloops/ zypper mr -r 2
chroot /tmp/wkmntloops/ zypper mr --no-gpgcheck 1
chroot /tmp/wkmntloops/ zypper mr --no-gpgcheck 2
chroot /tmp/wkmntloops/ zypper lr
chroot /tmp/wkmntloops/ zypper --non-interactive refresh
chroot /tmp/wkmntloops/ zypper --non-interactive update
chroot /tmp/wkmntloops/ zypper --non-interactive install $list_pkt
chroot /tmp/wkmntloops/ grub2-install --no-floppy --modules=part_gpt --target=i386-pc /dev/loop0

KERN="noquiet console=ttyS0 console=tty1 earlyprintk=serial net.ifnames=0 ipv6.disable=1"
printf "\nGRUB_CMDLINE_LINUX_DEFAULT=\"%s\"\n" "$KERN" \
        > /tmp/wkmntloops/etc/default/grub

version=$(ls /tmp/wkmntloops/lib/modules/)
if [ ! -e /tmp/wkmntloops/boot/vmlinuz-${version} ]; then
  echo ERROR:
  ls /tmp/wkmntloops/lib/modules/
  ls /tmp/wkmntloops/boot/
  umount /tmp/wkmntloops/{dev,proc,sys}
  exit 1
fi
echo $version
list="virtio_scsi virtio_blk virtio_console"
chroot /tmp/wkmntloops/ dracut --add-drivers "$list" --force /boot/initrd-${version}.img $version

chroot /tmp/wkmntloops/ grub2-mkconfig -o /boot/grub2/grub.cfg
uuid=$(blkid | grep cloonix)
uuid=${uuid#*UUID=\"}
uuid=${uuid%%\"*}
echo $uuid
sync /dev/loop0
umount /tmp/wkmntloops/{dev,proc,sys}
#-----------------------------------------------------------------------#
cat > /tmp/wkmntloops/etc/fstab  << EOF
UUID=$uuid  /            ext4     defaults                      0 0 
proc        /proc        proc     nosuid,noexec,nodev           0 0
sysfs       /sys         sysfs    nosuid,noexec,nodev           0 0
devpts      /dev/pts     devpts   nosuid,noexec,gid=5,mode=620  0 0
tmpfs       /run         tmpfs    defaults                      0 0
devtmpfs    /dev         devtmpfs mode=0755,nosuid              0 0
EOF
#-----------------------------------------------------------------------#
chroot /tmp/wkmntloops/ passwd root <<EOF
root
root
EOF
#-----------------------------------------------------------------------#
cat > /tmp/wkmntloops/etc/systemd/system/serial-getty@hvc0.service << EOF
[Service]
ExecStart=-/sbin/agetty -a root hvc0
Type=idle
Restart=always
RestartSec=0
EOF
#-----------------------------------------------------------------------#
cd /tmp/wkmntloops/etc/systemd/system/getty.target.wants
ln -s /etc/systemd/system/serial-getty@hvc0.service serial-getty@hvc0.service
cd ${HERE}
#-----------------------------------------------------------------------#
fct_umount_wkmntloops
#-----------------------------------------------------------------------#
qemu-img convert -O qcow2 $ROOTFS ${ROOTFS}.qcow2
rm -f $ROOTFS
#-----------------------------------------------------------------------#

